import { _decorator, Component, Vec3, instantiate, Node, UITransform, Sprite } from "cc"
import { Prefab } from "cc"
import { GlobalData } from "../GlobalData"
import ResManager from "../framework/ResManager"
const { ccclass, property } = _decorator

@ccclass("ItemBase")
export class ItemBase extends Component {
  protected sName: string = ""

  public maxHp: number = 0

  protected currHp: number = 0

  protected nodeHp: Node = null

  protected nodeCurrHp: Node = null

  protected characterData: any = null

  protected currAttack: number = 0

  protected attackSpeed: number = 0

  protected attackScope: number = 0

  public beAttackScope: number = 0

  protected moveSpeed: number = 0

  /**是否攻击动作 */
  protected isAttackAction: boolean = false
  //攻击cd冷却
  protected isAttackCd: boolean = false

  public isEnemy: boolean = false

  protected rScale:number = 0

  // protected healthRecoveryRate:number = 0
  /**是否是无敌状态 */
  protected isInvincible: boolean = false

  protected _isDie: boolean = false

  public set isDie(v: boolean) {
    this._isDie = v

    if (this._isDie) {
      this.showDie()
    }
  }
  public get isDie() {
    return this._isDie
  }

  onBeAttack(attack: number, nodeBeAttack?: Node) {
    if (this.isInvincible) return
    if (this.isDie) return
    this.changeHp(attack)
  }

  protected showDie() {}

  /**
   *
   * @param value
   * @param target
   * @returns
   */
  protected changeHp(value: number) {
    if (!GlobalData.instance.isGameStart || GlobalData.instance.isGameOver || GlobalData.instance.isGamePause) return
    value = Math.round(value)

    this.currHp -= value
    this.onChangeHp(this.currHp)
  }

  protected onChangeHp(hp: number) {
    if (!this.maxHp) return
    this.currHp = hp
    if (this.currHp > this.maxHp) {
      this.currHp = this.maxHp
    }
    if (this.currHp <= 0) {
      this.currHp = 0
      this.isDie = true
    }
  }

  /**检测节点是否可以攻击 */
  checkNodeCanBeAttack(node: Node) {
    if (!node || !node.isValid) return false
    let item = node.getComponent(ItemBase)
    if (!item || item.isDie) {
      return false
    }
    return true
  }


  public getCurrTextHp(){
    return this.currHp
  }


}
